package com.pxmeta.app.controller.community;

import cn.dev33.satoken.annotation.SaCheckDisable;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.enums.DynamicEnum;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.dynamic.entity.Topic;
import com.pxmeta.dynamic.entity.TopicCategory;
import com.pxmeta.dynamic.entity.SearchHistory;
import com.pxmeta.dynamic.entity.dto.TopicDTO;
import com.pxmeta.dynamic.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/community/topic")
@RequiredArgsConstructor
@Api(tags = "广场话题",value = "广场话题")
public class TopicController {

    private final ITopicService topicService;
    private final ITopicCategoryService categoryService;
    private final ITopicDynamicService topicDynamicService;
    private final ITopicUserFollowService topicUserFollowService;
    private final ISearchHistoryService searchHistoryService;


    @SaCheckLogin
    @ApiOperation("获取圈子下的话题")
    @GetMapping("/getHotTopicByCircle")
    public AjaxResult<List<TopicDTO>> getTopicByCircle(@RequestParam Long circleId) {
        Long userId = SecurityUtils.getUserId();
        List<TopicDTO> list = topicService.getHotTopicByCircle(userId, circleId);
        return AjaxResult.success(list);
    }

    @SaCheckLogin
    @PostMapping("/addTopic")
    @ApiOperation("添加话题")
    @SaCheckDisable
    public AjaxResult<Topic> addTopic(@RequestBody TopicDTO dto){
        Long userId = SecurityUtils.getUserId();
        Topic topic = topicService.add(dto, userId);
        return AjaxResult.success(topic);
    }

    @SaCheckLogin
    @ApiOperation("搜索话题")
    @GetMapping("/search")
    public AjaxResult<List<TopicDTO>> search(@RequestParam Long circleId,
                             @RequestParam String name){

        Long userId = SecurityUtils.getUserId();
        List<TopicDTO> list = topicService.search(userId,circleId, name);
        return AjaxResult.success(list);
    }

    @SaCheckLogin
    @ApiOperation("获取话题分类")
    @GetMapping("/getCategory")
    public AjaxResult<List<TopicCategory>> getCategory(@RequestParam Long circleId){
        List<TopicCategory> list = categoryService.getCategoryByCircleType(circleId);
        return AjaxResult.success(list);
    }

    @SaCheckLogin
    @ApiOperation("根据话题分类获取话题列表")
    @GetMapping("/getTopicByCategoryId")
    public AjaxResult<List<TopicDTO>> getTopicByCategoryId(@RequestParam Long circleId,
                                           @RequestParam Long categoryId){

        Long userId = SecurityUtils.getUserId();
        List<TopicDTO> list = topicService.getByCategoryId(userId, circleId, categoryId);
        return AjaxResult.success(list);
    }


    @SaCheckLogin
    @GetMapping("/getSearchHistory")
    @ApiOperation("获取话题搜索历史")
    public AjaxResult<List<SearchHistory> > getSearchHistory(@RequestParam Long circleId){

        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<SearchHistory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SearchHistory::getUserId, userId)
                .eq(SearchHistory::getCircleId, circleId)
                .eq(SearchHistory::getType, DynamicEnum.TYPE_TOPIC.getType())
                .isNotNull(SearchHistory::getTargetId)
                        .orderByDesc(SearchHistory::getUpdateTime);
        List<SearchHistory> list = searchHistoryService.list(wrapper);
        return AjaxResult.success(list);
    }


    @SaCheckLogin
    @GetMapping("/followTopic")
    @ApiOperation("关注话题")
    @SaCheckDisable
    public AjaxResult followTopic(@RequestParam Long topicId){
        Long userId = SecurityUtils.getUserId();
        Topic topic = topicService.getById(topicId);
        if (null == topic){
            return AjaxResult.error( HttpStatus.PARAMS_ERROR,"话题不存在！");
        }
        topicUserFollowService.follow(userId,topicId,topic.getCircleId());
        return AjaxResult.success();
    }


    @GetMapping("/detail")
    @SaCheckLogin
    @ApiOperation("获取话题详情")
    public AjaxResult<TopicDTO> detail(@RequestParam Long topicId){
        Long userId = SecurityUtils.getUserId();
        Topic topic = topicService.getById(topicId);
        TopicDTO dto = new TopicDTO();
        BeanUtils.copyProperties(topic,dto);
        boolean follow = topicUserFollowService.isFollow(userId, topicId);
        dto.setIsFollow(follow);
        dto.setDynamicCount(topicDynamicService.getDynamicCount(topicId));
        return AjaxResult.success(dto);
    }


    @SaCheckLogin
    @GetMapping("/unfollowTopic")
    @ApiOperation("取消关注")
    @SaCheckDisable
    public AjaxResult unfollowTopic(@RequestParam Long topicId){
        Long userId = SecurityUtils.getUserId();
        topicUserFollowService.unfollow(userId,topicId);
        return AjaxResult.success();
    }


}
